/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.preference;

import net.jonbuck.tassoo.eventmanager.EventManager;
import net.jonbuck.tassoo.eventmanager.EventManagerImpl;
import net.jonbuck.tassoo.eventmanager.events.TaskCommentPreferencesUpdatedEvent;
import net.jonbuck.tassoo.ui.nl.Messages;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
 * 
 * 
 * @since 1.0.0
 */
public class CommentsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	public static final String NEWER_COMMENTS_FIRST = "newer.comments.first"; //$NON-NLS-1$

	public static final String OLDER_COMMENTS_FIRST = "older.comments.first"; //$NON-NLS-1$

	private Button btnNewCommentsFirst;

	private Button btnOlderCommentsFirst;

	private EventManager eventManager = EventManagerImpl.getInstance();

	private IPreferencesService preferenceService;

	private Preferences tassooNode;

	/**
	 * 
	 */
	public CommentsPreferencePage() {
		super();
	}

	/**
	 * 
	 */
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = layout.marginWidth = 0;
		container.setLayout(layout);

		Group directionGroup = new Group(container, SWT.NONE);
		directionGroup.setText(Messages.commentspreferencepage_directionGroup_text);
		directionGroup.setLayout(new GridLayout(1, false));
		directionGroup.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));

		btnNewCommentsFirst = new Button(directionGroup, SWT.RADIO);
		btnNewCommentsFirst.setText(Messages.commentspreferencepage_btnnewcommentsfirst_text);
		btnNewCommentsFirst.setSelection(tassooNode.getBoolean(NEWER_COMMENTS_FIRST, true));

		btnOlderCommentsFirst = new Button(directionGroup, SWT.RADIO);
		btnOlderCommentsFirst.setText(Messages.commentspreferencepage_btnoldercommentsfirst_text);
		btnOlderCommentsFirst.setSelection(tassooNode.getBoolean(OLDER_COMMENTS_FIRST, false));

		return container;
	}

	/**
	 * 
	 */
	public void init(IWorkbench workbench) {
		this.setDescription(Messages.commentspreferencepage_description);
		this.noDefaultAndApplyButton();
		preferenceService = Platform.getPreferencesService();
		tassooNode = preferenceService.getRootNode().node(ConfigurationScope.SCOPE).node("net.jonbuck.tassoo");
	}

	/**
	 * 
	 */
	public boolean performOk() {
		tassooNode.putBoolean(NEWER_COMMENTS_FIRST, btnNewCommentsFirst.getSelection());
		tassooNode.putBoolean(OLDER_COMMENTS_FIRST, btnOlderCommentsFirst.getSelection());
		try {
			tassooNode.flush();
			eventManager.fireEvent(new TaskCommentPreferencesUpdatedEvent());
		} catch (BackingStoreException bse) {
			// TODO Handle exception correctly and log information
			bse.printStackTrace();
		}
		return super.performOk();
	}

}
